/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tlf.dyjc5.servlet.data;

import com.tlf.dyjc5.service.RsjService;
import com.tlf.dyjc5.service.YsjService;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dusin.db.exception.AlreadLockedException;
import org.dusin.web.util.JsonUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author tengyu
 */
public class LockServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        YsjService ysjService = (YsjService)context.getBean("ysjServiceProxy");
        RsjService rsjService = (RsjService)context.getBean("rsjServiceProxy");
        
        String currentOrgCode = (String)request.getSession().getAttribute("currentOrgCode");
        try {
            String command = request.getParameter("cmd");
            if("lockYPoints".equals(command)){
                String pointIds = request.getParameter("pointIds");
                String date = request.getParameter("date");
                ysjService.trLockByPointIds(currentOrgCode,pointIds, date);                
            }else if("unlockYPoints".equals(command)){
                String pointIds = request.getParameter("pointIds");
                String date = request.getParameter("date");                
                ysjService.trUnLockByPointIds(currentOrgCode,pointIds, date);
            }else if("lockYOrg".equals(command)){
                String date = request.getParameter("date");
//                String range = request.getParameter("range");
//                String reportStatus = request.getParameter("reportStatus");
                int  range = -1;
                int reportStatus = 1;    //
                //ysjService.trLockByOrg(currentOrgCode, date,Integer.parseInt(range),Integer.parseInt(reportStatus));
                ysjService.trLockByOrg(currentOrgCode, date);
            }else if("unlockYOrg".equals(command)){
                String date = request.getParameter("date");
                //String range = request.getParameter("range");
                //String reportStatus = request.getParameter("reportStatus");
                int  range = -1;
                int reportStatus = 1;    //
                //ysjService.trUnLockByOrg(currentOrgCode, date,Integer.parseInt(range),Integer.parseInt(reportStatus));
                ysjService.trUnLockByOrg(currentOrgCode, date);
            }else if("lockRPoints".equals(command)){
                String pointIds = request.getParameter("pointIds");
                String date = request.getParameter("date");
                rsjService.trLockByPointIds(pointIds, date);                
            }else if("unlockRPoints".equals(command)){
                String pointIds = request.getParameter("pointIds");
                String date = request.getParameter("date");                
                rsjService.trUnLockByPointIds(pointIds, date);
            }else if("lockROrg".equals(command)){
                String date = request.getParameter("date");
//                String range = request.getParameter("range");
//                String reportStatus = request.getParameter("reportStatus");
                String range = "-1";
                String reportStatus = "-1";    //数据一次性全部锁定，不可以分别对子口径，母口径、上报、观测分别锁定,降低编程复杂性
                rsjService.trLockByOrg(currentOrgCode, date,Integer.parseInt(range),Integer.parseInt(reportStatus));
            }else if("unlockROrg".equals(command)){
                String date = request.getParameter("date");
                //String range = request.getParameter("range");
                //String reportStatus = request.getParameter("reportStatus");
                String range = "-1";
                String reportStatus = "-1";    //数据一次性全部锁定，不可以分别对子口径，母口径、上报、观测分别解锁 
                rsjService.trUnLockByOrg(currentOrgCode, date,Integer.parseInt(range),Integer.parseInt(reportStatus));
                
            }else if("copyYOrg".equals(command)){   //从ysj表中拷贝到ysjLock表，只拷贝上报数据
                String date = request.getParameter("date");
                int range = -1;
                int reportStatus = -1;    //数据一次性全部锁定，不可以分别对子口径，母口径、上报、观测分别锁定                
                ysjService.trCopyByOrg(currentOrgCode, date,range,reportStatus);
            }else if("copyROrg".equals(command)){   //从ysj表中拷贝到ysjLock表，只拷贝上报数据
                String date = request.getParameter("date");
                int range = -1;
                int reportStatus = -1;    //数据一次性全部锁定，不可以分别对子口径，母口径、上报、观测分别锁定                
                rsjService.trCopyByOrg(currentOrgCode, date,range,reportStatus);
            }
            
            out.println(JsonUtil.createSuccessJsonResponse(""));           
        }catch(AlreadLockedException e){
            out.println(JsonUtil.createFailureJsonResponse(e.getMessage()));
            //e.printStackTrace();
        }catch(org.springframework.dao.DuplicateKeyException e){
            out.println(JsonUtil.createFailureJsonResponse("插入了重复数据"));
            e.printStackTrace();
        }catch(Exception e){
            out.println(JsonUtil.createFailureJsonResponse("不明操作错误"));
            e.printStackTrace();
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
